package cn.gcBlock.rest;

import cn.gcBlock.service.BlockNodeService;
import cn.gcBlock.service.BlockNodeTransService;
import cn.gcBlock.utils.HashUtils;
import cn.gcBlock.po.constant.NodeType;
import cn.gcBlock.dto.NodeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author：dapan
 * @Description:cadota
 * @Date:Create in 17:39 2018/5/5
 * @Modifird By:
 */
@Api(tags = "区块帮助模块（注册钱包，验证签名）")
@RestController
@RequestMapping
public class RestBlockHelpServiceImpl implements RestBlockHelpService {
    @Resource
    BlockNodeService blockNodeService;
    @Resource
    BlockNodeTransService blockNodeTransService;

    @ApiOperation(value = "生成签名单")
    public ResponseEntity<NodeDTO> getDetail(@RequestBody NodeDTO resource) {
        resource.setTimestamp(System.nanoTime());
        if(NodeType.GUARANTEE.getCode().equals(resource.getRemark())){
            resource.setType(NodeType.GUARANTEE.getCode());
        }
        if(NodeType.CONSIGNOR.getCode().equals(resource.getRemark())){
            resource.setType(NodeType.CONSIGNOR.getCode());
        }
        if(StringUtils.isNotBlank(resource.getDataBlock())){
            String hashRemark = HashUtils.hashKeyForDisk(resource.getDataBlock());
            resource.setRemark(hashRemark);
            resource.setCoin(0);
            resource.setType(NodeType.NEWS.getCode());
        }
        NodeDTO result = blockNodeService.sign(resource);
        List<NodeDTO> wallets = blockNodeService.findWallet(result.getToPublicKey());
        if(wallets!=null&&wallets.size()>0){
            result.setToName(wallets.get(0).getBlockName());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @ApiOperation(value = "生成签名单并发送")
    public ResponseEntity<NodeDTO> signPost(@RequestBody NodeDTO resource) {
        resource.setTimestamp(System.nanoTime());
        if(NodeType.GUARANTEE.getCode().equals(resource.getRemark())){
            resource.setType(NodeType.GUARANTEE.getCode());
        }
        if(NodeType.CONSIGNOR.getCode().equals(resource.getRemark())){
            resource.setType(NodeType.CONSIGNOR.getCode());
        }
        if(StringUtils.isNotBlank(resource.getDataBlock())){
            String hashRemark = HashUtils.hashKeyForDisk(resource.getDataBlock());
            resource.setRemark(hashRemark);
            resource.setCoin(0);
            resource.setType(NodeType.NEWS.getCode());
        }
        NodeDTO result = blockNodeService.sign(resource);
        List<NodeDTO> wallets = blockNodeService.findWallet(result.getToPublicKey());
        if(wallets!=null&&wallets.size()>0){
            result.setToName(wallets.get(0).getBlockName());
        }
        blockNodeTransService.transBlock(result);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    
    @ApiOperation(value = "生成钱包地址")
        public ResponseEntity address(@RequestParam String symbol) {
        //注册
        NodeDTO entity = blockNodeService.registerBlockAddress(symbol);
        //签名
        entity.setTimestamp(System.nanoTime());
        entity.setPrivateKey(entity.getPrivateKey());
        entity.setType(NodeType.WALLET.getCode());
        NodeDTO result = blockNodeService.sign(entity);
        //传递
        blockNodeTransService.transBlock(result);
        return ResponseEntity.ok(result);
    }

    
    @ApiOperation(value = "创建")
    public ResponseEntity create(@Validated @RequestBody NodeDTO resource) {
        resource.setTimestamp(System.nanoTime());
        if(NodeType.GUARANTEE.getCode().equals(resource.getRemark())){
            resource.setType(NodeType.GUARANTEE.getCode());
        }
        if(NodeType.CONSIGNOR.getCode().equals(resource.getRemark())){
            resource.setType(NodeType.CONSIGNOR.getCode());
        }
        if(StringUtils.isNotBlank(resource.getDataBlock())){
            String hashRemark = HashUtils.hashKeyForDisk(resource.getDataBlock());
            resource.setRemark(hashRemark);
            resource.setCoin(0);
            resource.setType(NodeType.NEWS.getCode());
        }
        NodeDTO signedNode = blockNodeService.sign(resource);
        blockNodeTransService.transBlock(signedNode);
        return new ResponseEntity(signedNode, HttpStatus.CREATED);
    }

}
